// packageI/public_welfare_fund/record/record.js
const app = getApp();
Page({
    data: {
        title: "",
        type: 1, // 1 公益支出记录 2公益明细
  
        finished: false,
        loading: false,
        page: 1,
  
        expendList: [],
        fundList: []
    },
    onLoad: function (options) {
        this.setData({type: options.type})
    },

    onShow: function () {
        if (this.data.type == 1) {
            wx.setNavigationBarTitle({title: '支出记录',})
            this.getExpendData();
          } else {
            wx.setNavigationBarTitle({title: '公益明细',})
            this.getFundData();
          }
    },

    getExpendData() {
        let urlStr = app.getNetAddresss("plugin.public-fund.frontend.controllers.fund.expendList");
        wx.showLoading({ title: 'loading' });
        this.setData({loading: true});
        app._getNetWork({
            url: urlStr,
            data: {
              page: this.data.page,
            },
            success: (res) => {
                if (res.data.result !== 1) {
                    wx.showToast({ title: res.data.msg});
                    return;
                }
                let data = res.data.data.list;
                if (data.current_page >= data.last_page || data.data.length < data.per_page) {
                    this.setData({finished: true});
                }
                this.setData({
                    page: ++this.data.page,
                    expendList: this.data.page > 1 ? this.data.expendList.concat(data.data) : data.data
                })
            },
            complete: () => {
                wx.hideLoading();
                this.setData({loading: false});
            }
        })
    },


    getFundData() {
        let urlStr = app.getNetAddresss("plugin.public-fund.frontend.controllers.fund.amountList");
        wx.showLoading({ title: 'loading' })
        this.setData({loading: true});
        app._getNetWork({
            url: urlStr,
            data: {
              page: this.data.page,
            },
            success: (res) => {
                if (res.data.result !== 1) {
                    wx.showToast({ title: res.data.msg});
                    return;
                }
                let data = res.data.data.list;
                if (data.current_page >= data.last_page || data.data.length < data.per_page) {
                    this.setData({finished: true});
                }
                this.setData({
                    page: ++this.data.page,
                    fundList: this.data.page > 1 ? this.data.fundList.concat(data.data) : data.data
                })
            },
            complete: () => {
                wx.hideLoading();
                this.setData({loading: false});
            }
        })
      },
    onReachBottom: function () {
        if (this.data.finished || this.data.loading) {
            return false;
        }
        if (this.data.type == 1) {
            this.getExpendData();
        } else {
            this.getFundData();
        }
    },

    onShareAppMessage: function () {

    }
})